$(function () {
    initCourseCatalog();
});
/**
 * 初始化课程目录
 */
var materialId;
function initCourseCatalog() {
    var settings = {
        async: {
            dataType: 'json',
            dataFilter: function (treeId, treeNode, response) {
                if (response.success) {
                    var list = response.entity;
                    if(list!=null && list.length>0){
                        $.each(list,function (index,em) {
                            let videoDuration = em.videoDuration;
                            if(em.materialId==0&&em.nodeType==2){
                                em.catalogName = em.catalogName+'(没有学习资源)';
                            }else if(em.materialTypeKey=="LIVE"&&em.nodeType==2){
                                let strTime = longTimeToStr(videoDuration);
                                em.catalogName = "[直播-"+strTime+"] " +em.catalogName+' ['+em.lessonStartTime.replace(/-/g,"/")+' - '+em.lessonEndTime.replace(/-/g,"/")+']';
                            }else if(em.nodeType==2){
                                let strTime = longTimeToStr(videoDuration);
                                var type ="";
                                switch ( em.materialTypeKey) {
                                    case "VIDEO":
                                        type="视频";
                                        break;
                                    case "AUDIO":
                                        type="音频";
                                        break;
                                    case "ARTICLE":
                                        type="文章";
                                        break;

                                }
                                em.catalogName = "["+type+"-"+strTime+"] " +em.catalogName;
                                if (em.questionIds!=null&&em.questionIds!=""){
                                    em.catalogName ="[随堂练习] "+em.catalogName;
                                }
                            }
                        });
                    }else{
                        $("body").html("<div class='noDataTipsBox'>" +
                            "                        <p><span class='c-4e fs16'>亲，还没有数据~~~</span></p>" +
                            "                    </div>")
                    }
                    return response.entity;
                } else {
                    icModale(0, response.message);
                }
            },
            enable: true,
            type: 'post',
            url: baseloaction + "/admin/course/catalog/findCourseCatalog?courseId=" +courseId
        },
        check: {
            enable: true,
            chkStyle: "radio",
            radioType:"all",
            autoCheckTrigger: false,
            checkboxType:{ "Y" : "", "N" : "" }
            },
        data: {
            key: {
                name: 'catalogName',
                title: 'catalogName',
                mId: 'mouldId'
            },
            simpleData: {
                enable: true,
                idKey: 'id',
                pIdKey: 'parentId',
                rootPId: null
            }
        },
        edit: {
            drag: {
                isMove: true
            },
            enable: false,
        },
        view: {
            fontCss: function (treeId, treeNode) {
                if (typeof treeNode.highlight == 'undefined' || treeNode.highlight == false) {
                    if(treeNode.materialId==0&&treeNode.nodeType==2){
                        return {color:"#dda525", "font-weight":"normal"};
                    }else{
                        return {color:"#333", "font-weight":"normal"};
                    }
                } else {
                    return {color: "#A60000", "font-weight": "bold"};
                }
            },
            showLine: true,
            selectedMulti: false
        },
        callback: {
            onAsyncError: function (event, treeId, treeNode, request, status) {//异步加载数据失败
                icModale(0, "系统错误，章节获取失败");
            },
            onAsyncSuccess: function (event, treeId, treeNode, msg) {//异步加载数据成功
                var treeObj = $.fn.zTree.getZTreeObj(treeId);
                showHide(treeObj);
            },
            onCheck:function(event,treeId,treeNode){
                if (treeNode.materialId != 0) {
                    $("#materialId").val(treeNode.materialId);
                    $("#catalogId").val(treeNode.id);
                    $("#catalogName").val(treeNode.catalogName);
                }
            }
        }
    };
    //初始化权限树
    $.fn.zTree.init($('#ztree'), settings);
}

/**
 * 显示和隐藏数据
 * @param treeObj
 */
function showHide(treeObj) {
    if ((treeObj).getNodes().length != 0) {
        $('#'+treeObj.setting.treeId).removeClass("hide");
        $('#'+treeObj.setting.treeId).next('div.noDataTipsBox').addClass('hide');
        treeObj.expandAll(true);
    } else {
        $('#'+treeObj.setting.treeId).addClass("hide");
        $('#'+treeObj.setting.treeId).next('div.noDataTipsBox').removeClass('hide');
    }
}